package tesys.business;

import java.util.List;

import tesys.application.Main;
import tesys.application.Services;
import tesys.objects.Iteration;
import tesys.persistence.DataAccessStub;

public class AccessIterations
{
	private DataAccessStub dataAccess;
	private List<Iteration> Iterations;
	private Iteration Iteration;
	private int currentIteration;

	public AccessIterations()
	{
		dataAccess = (DataAccessStub) Services.getDataAccess(Main.dbName);
		Iterations = null;
		Iteration = null;
		currentIteration = 0;
	}

	public Iteration getSequential()
	{
		if (Iterations == null)
		{
			Iterations = dataAccess.getIterationSequential();
			currentIteration = 0;
		}
		if (currentIteration < Iterations.size())
		{
			Iteration = (Iteration) Iterations.get(currentIteration);
			currentIteration++;
		}
		else
		{
			Iterations = null;
			Iteration = null;
			currentIteration = 0;
		}
		return Iteration;
	}

	public Iteration getRandom(String iterationID, String projectID)
	{
		Iteration = null;
		if (IterationID.trim().equals(""))
		{
			//System.out.println("*** Invalid Iteration id");
		}
		else
		{
			Iterations = dataAccess.getIterationRandom(new Iteration(iterationID, projectID));
			if (Iterations.size()==1)
			{
				Iteration = (Iteration) Iterations.get(0);
			}
		}
		return Iteration;
	}

	public String insertIteration(Iteration currentIteration)
	{
		return dataAccess.insertIteration(currentIteration);
	}

	public String updateIteration(Iteration currentIteration)
	{
		return dataAccess.updateIteration(currentIteration);
	}

	public String deleteIteration(Iteration currentIteration)
	{
		return dataAccess.deleteIteration(currentIteration);
	}
}
